#!/usr/bin/python
import time
from random import uniform
# position : [up, down, left, right]
moveList = {
1 : (4, 0, 0, 2),
2 : (5, 0, 1, 3),
3 : (6, 0, 2, 0),
4 : (7, 1, 0, 5),
5 : (8, 2, 4, 6),
6 : (9, 3, 5, 0),
7 : (0, 4, 0, 8),
8 : (0, 5, 7, 9),
9 : (0, 6, 8, 0)
}

Nhits = {
1 : 0,
2 : 0,
3 : 0,
4 : 0,
5 : 0,
6 : 0,
7 : 0,
8 : 0,
9 : 0
}



## create record
#iter_time = []
#record = {}
#tmp = []
#for i in Nhits.keys():
#	record[i] = tmp

N = 4000
startPosition = 9

# start simulate
Nhits[startPosition] += 1
position = startPosition

#iter_time.append(1)
#for i in Nhits.keys():
#	record[i].append(Nhits[i])

for i in range(1, N):
	direction = int(uniform(0, 4)) # 0:up, 1:down, 2:left, 3:right
	nextPosition =  moveList[position][direction]
	if nextPosition != 0:
		position = nextPosition

	Nhits[position] += 1

#	iter_time.append(i)
#	for j in Nhits.keys():
#		record[j].append(Nhits[j])

	print position, Nhits.values()
	#time.sleep(.005)

# end

# convert array format for gnuplot
plotarray = []
for i in moveList.keys():
	print i, '\t', float(Nhits[i])/float(N)
	plotarray.append([i, float(Nhits[i])/float(N)])

import Gnuplot as gp
plotItem = gp.PlotItems.Data(plotarray, with_='boxes')
g = gp.Gnuplot()
g('set yrange [0:1]')
g.xlabel('Position')
g.ylabel('Probability')
g.plot(plotItem)

#site_hist = []
#for site in Nhits.keys():
#	tmp = []
#	for i in range(len(record[site])):
#		tmp.append([i, record[site][i]])
#	site_hist.append(tmp)
#
#
#plotItems = []
#for i in Nhits.keys():
#	plotItems.append(gp.PlotItems.Data(site_hist[i-1]))
#
#g_iter = gp.Gnuplot()
#g_iter.xlabel('Move times')
#g_iter.ylabel('Count')
#for i in plotItems:
#	g_iter.replot(i)
raw_input()
